package com.music.service.Impl;

import com.music.mapper.*;
import com.music.pojo.*;
import com.music.service.UserService;
import com.music.utils.QueryVo;

import com.music.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private SingerMapper singerMapper;
    @Autowired
    private SingleSongMapper singleSongMapper;
    @Autowired
    private PlaylistMapper playlistMapper;
    @Autowired
    private AlbumMapper albumMapper;
    @Override
    public QueryVo find_comprehensive(String param) {
        QueryVo queryVo = new QueryVo();
        Singer singer = singerMapper.queryByName(param);//查询名字绝对符合且人气最高的
        List<SingleSong> singleSongs = singleSongMapper.findWithCategorysAndSingerAndAlbum(param);
        List<Playlist> playlists = playlistMapper.queryByPlaylistNameOrClassify(param);//模糊查询歌单名或其精确查询所属分类
        List<Album> albums = albumMapper.queryByAlbumNameOrSingerName(param);//模糊查询专辑名或精确查询歌手名
        queryVo.setSinger(singer);
        queryVo.setSingleSongs(singleSongs);
        queryVo.setPlaylists(playlists);
        queryVo.setAlbums(albums);
        return queryVo;

    }

    @Override
    public UserInfo find_login(String email, String password) {
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andEmailEqualTo(email);
        criteria.andPasswordEqualTo(password);

        UserInfo userInfo = userInfoMapper.selectByExample(userInfoExample).get(0);
        return userInfo;
    }

    @Override
    public UserInfo find_verifyEmail(String email) {
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andEmailEqualTo(email);

        return userInfoMapper.selectByExample(userInfoExample).get(0);
    }

    @Override
    public void add_register(UserInfo userInfo) {
        userInfoMapper.insertSelective(userInfo);
    }

    @Override
    public ResultInfo verify_registerCode(HttpServletRequest request,String code) {
        ResultInfo resultInfo = new ResultInfo();
        String code1 = (String) request.getSession().getAttribute("code");
        if (code1==null){
            code1="";
        }
        if (code1.equals(code)){
            resultInfo.setFlag(true);
        }else {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("验证码错误或失效");
        }
        return resultInfo;
    }

    @Override
    public void addUserSinger(Integer sid, Integer uid) {
        userInfoMapper.addUserSinger(sid,uid);
    }

    @Override
    public void removeUserSinger(Integer sid, Integer uid) {
        userInfoMapper.removeUserSinger(sid,uid);
    }


    @Override
    public List<UserInfo> find_all() {
        return userInfoMapper.selectByExample(new UserInfoExample());
    }

    @Override
    public UserInfo findUserMusicDetail(Integer uid) {
        UserInfo userInfo = new UserInfo();
        List<Playlist> userPlayLists = playlistMapper.findByUid(uid);
        List<Playlist> userFavoritePlayLists = playlistMapper.findUserFavoritePlayLists(uid);
        List<Album> albumList = albumMapper.findFavoriteByUid(uid);
        userInfo.setPlaylists(userPlayLists);
        userInfo.setFavoritePlayLists(userFavoritePlayLists);
        userInfo.setFavoriteAlbums(albumList);
        return userInfo;
    }
}
